package com.kanshu.ksgb.fastread.doudou.common.view;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.common.util.DisplayUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by wcy on 2017\9\13 0013.
 */
public class CustomDialog extends Dialog {

    Callback mCallback;
    @BindView(R.id.title)
    TextView mTitle;
    @BindView(R.id.content)
    TextView mContent;
    @BindView(R.id.divider)
    View mDivider;
    @BindView(R.id.cancel)
    Button mCancel;
    @BindView(R.id.vertical_divider)
    View mVerticalDivider;
    @BindView(R.id.sure)
    Button mSure;

    public CustomDialog(@NonNull Context context) {
        super(context, R.style.dialog_style);
        init();
    }

    private void init() {
        setContentView(R.layout.dialog_comm_layout);
        ButterKnife.bind(this);
    }

    public void setTitle(CharSequence s) {
        mTitle.setText(s);
    }

    public void setContent(CharSequence s) {
        mContent.setText(s);
    }

    public void goneTitle() {
        DisplayUtils.gone(mTitle);
    }

    public void invisibleTitle() {
        DisplayUtils.invisible(mTitle);
    }

    public void showOnlySure() {
        DisplayUtils.gone(mCancel);
        DisplayUtils.gone(mVerticalDivider);
    }

    public CustomDialog setSureBtnText(CharSequence s) {
        mSure.setText(s);
        return this;
    }

    public CustomDialog setCancelBtnText(CharSequence s) {
        mCancel.setText(s);
        return this;
    }

    public void setCallback(Callback callback) {
        mCallback = callback;
    }

    @OnClick({R.id.cancel, R.id.sure})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.sure:
                if (mCallback != null) {
                    mCallback.onSure(this);
                } else {
                    dismiss();
                }
                break;
            case R.id.cancel:
                if (mCallback != null) {
                    mCallback.onCancel(this);
                } else {
                    dismiss();
                }
                break;
            default:
                break;
        }
    }

    public interface Callback {
        void onSure(Dialog dialog);

        void onCancel(Dialog dialog);
    }

    /**
     * @param activity
     * @param title
     * @param content
     * @param callback
     * @return
     */
    public static CustomDialog show(Activity activity, CharSequence title, CharSequence content, Callback callback, boolean isShowOnlySure) {
        if (activity == null || activity.isFinishing()) {
            return null;
        }
        final CustomDialog dlg = new CustomDialog(activity);
        if (TextUtils.isEmpty(title)) {
            dlg.goneTitle();
        } else {
            dlg.setTitle(title);
        }
        dlg.setContent(content);
        dlg.setCallback(callback);
        if (isShowOnlySure) {
            dlg.showOnlySure();
        }
        dlg.show();
        return dlg;
    }

    public static CustomDialog show(Activity activity, CharSequence title, CharSequence content, Callback callback, boolean isShowOnlySure, CharSequence cancelDesc, CharSequence sureDesc) {
        if (activity == null || activity.isFinishing()) {
            return null;
        }
        final CustomDialog dlg = new CustomDialog(activity);
        if (!TextUtils.isEmpty(cancelDesc)) {
            dlg.setCancelBtnText(cancelDesc);
        }
        if (!TextUtils.isEmpty(sureDesc)) {
            dlg.setSureBtnText(sureDesc);
        }
        if (TextUtils.isEmpty(title)) {
            dlg.goneTitle();
        } else {
            dlg.setTitle(title);
        }
        dlg.setContent(content);
        dlg.setCallback(callback);
        if (isShowOnlySure) {
            dlg.showOnlySure();
        }
        try {
            dlg.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dlg;
    }

    public static CustomDialog show(Activity activity, CharSequence title, CharSequence content, boolean isShowOnlySure) {
        return show(activity, null, content, null, isShowOnlySure);
    }

    public static CustomDialog show(Activity activity, CharSequence content, Callback callback, boolean isShowOnlySure) {
        return show(activity, null, content, callback, isShowOnlySure);
    }

    public static CustomDialog show(Activity activity, CharSequence content, boolean isShowOnlySure) {
        return show(activity, null, content, null, isShowOnlySure);
    }

    public TextView getContentTextView(){
        return mContent;
    }
}
